package com.rwlai.dianshang;

import com.rwlai.dianshang.domain.Order;
import com.rwlai.dianshang.domain.OrderItem;
import com.rwlai.dianshang.domain.Product;
import com.rwlai.dianshang.domain.User;
import com.rwlai.dianshang.repository.OrderRepository;
import com.rwlai.dianshang.repository.ProductRepository;
import com.rwlai.dianshang.repository.UserRepository;

import com.rwlai.dianshang.service.ShangchengService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import java.util.List;
import java.util.Optional;

@EnableJpaAuditing
@SpringBootApplication
public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

    @Bean
    public CommandLineRunner demo(ShangchengService shangchengService,UserRepository repository, OrderRepository orderRepository, ProductRepository productRepository) {
        return (args) -> {
            User u=new User();
            shangchengService.addUser(u);


            Product p = new Product("衬衫",100L,50,"SHANGYI","RWL");
            Product p2 = new Product("长裤",120L,100,"KUZI","HONGDOU");
            shangchengService.addProduct(p);
            shangchengService.addProduct(p2);

            Order o = new Order();
            o.getItems().add(new OrderItem(p, 3));
            o.getItems().add(new OrderItem(p2, 5));
            shangchengService.placeOrder(o,u);
            o = orderRepository.save(o);
            Order o2=shangchengService.getOrderById(o.getId());
            System.out.println(o2.getTotalPrice());

            Optional<OrderItem> opt=o.getItems().findById("2343434343");
            opt.ifPresent(x->System.out.println(x.getId()));
            for(OrderItem item:o.getItems().get()){
                System.out.println(item);
                System.out.println(item.getProduct().getName());
                //item.getParent().getTotal()
            }


            List<Order> orders=orderRepository.findByItemsProduct_Name("衬衫");
            System.out.println(orders.size());


        };
    }

}